package com.helloworld.bulletin.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.helloworld.common.dao.SimpleBizDao;
import com.cfw.util.GridUtil;
import com.helloworld.bulletin.dao.BulletinDao;
import com.helloworld.bulletin.service.BulletinService;

@Service(value = "bulletinService") 
public class BulletinServiceImpl implements BulletinService {
	private static final Logger logger = LoggerFactory.getLogger(BulletinServiceImpl.class);

	@Autowired 
	private BulletinDao bulletinDao;
	
	@Autowired
	private SimpleBizDao simpleBizDao;
	
	private static String[][] GRID_COL_MAP = {
		{"articleId", "ARTICLE_ID", GridUtil.DEF_KEY_TOKEN},
		{"title", "TITLE"},
		{"fixYn", "FIX_YN"},
		{"popupYn", "POPUP_YN"},
		{"dispLevel", "DISP_LEVEL"},
		{"regUser", "REG_USER"},
		{"regDateTime", "REG_DATETIME"},
		{"attachYn", "ATTACH_YN"}
	};
    
	@Override
	public Map<String, Object> list(Map<String, Object> param) {
		GridUtil.setupSearchParam(param, GRID_COL_MAP);

		GridUtil.setupParam(param, GRID_COL_MAP, simpleBizDao.select("bulletin.list.count", param));
		
		List<Map<String, Object>> rows = simpleBizDao.list("bulletin.list", param);
		
		Map<String, Object> map = GridUtil.getGridResultMap(param, rows, GRID_COL_MAP);
		
		return map;
	}

	@Override
	public Map<String, Object> select(Map<String, Object> param) {
		return simpleBizDao.select("bulletin.select", param);
	}
	
	@Override
	@Transactional
	public void save(Map<String, Object> param) {
		if (param == null) { return; }

		String articleId = (String)param.get("articleId");
		if (StringUtils.isNotEmpty(articleId)) { // 게시글번호가 있다면 수정
			logger.debug("*** " + param);
			
			simpleBizDao.update("bulletin.update", param);
		} else {
			String newSeq = simpleBizDao.newSeq("bulletin.newseq");
			param.put("articleId", newSeq);
			
			int dispLevel = 1;
			int seq = 1;
			String rootArticleId = newSeq;
			
			String parentArticleId = (String)param.get("parentArticleId");
			if (StringUtils.isNotEmpty(parentArticleId)) { // 답글이라면 하위글 정보 필요
				Map<String, Object> parent = simpleBizDao.select("bulletin.select.parent", param);
				
				if (parent != null) {
					if (parent.get("NEWSEQ") != null) { seq = ((BigDecimal)parent.get("NEWSEQ")).intValue(); }
					if (parent.get("DISP_LEVEL") != null) { dispLevel = ((BigDecimal)parent.get("DISP_LEVEL")).intValue(); }
					if (parent.get("ROOT_ARTICLE_ID") != null) { rootArticleId = (String)parent.get("ROOT_ARTICLE_ID"); }
				}
			} else {
				param.put("parentArticleId", param.get("articleId"));
			}
			
			param.put("dispLevel", dispLevel);
			param.put("seq", seq);
			param.put("rootArticleId", rootArticleId);
			
			logger.debug("*** " + param);
			
			simpleBizDao.insert("bulletin.insert", param);
			
			if (StringUtils.isNotEmpty(parentArticleId)) { // 답글이라면 하위글 순번을 일괄적으로 +1 처리 필요
				simpleBizDao.update("bulletin.incseq", param);
			}
		}
	}

	@Override
	public void delete(Map<String, Object> param) {
		simpleBizDao.delete("bulletin.delete", param);
	}

	@Override
	public Map<String, Object> selectAttach(Map<String, Object> param) {
		
		return null;
	}

}
